const Upload = require('../../libs/upload')

/**
 * 音乐模型
 */
class UserDemo {
    /**
     * 构造音乐对象
     * @param  {number} id       标识
     * @param  {string} name     用户
     * @param  {string} avatar   头像
     * @param  {string} nickname   昵称
     * @param  {number} listenCount   听歌数
     * @param  {number} fansCount   粉丝数
     * @return {Music}           音乐对象
     */
    constructor(id, name, avatar, nickname, listenCount, fansCount) {
        this.id = id
        this.name = name
        this.avatar = avatar
        this.nickname = nickname
        this.listenCount = listenCount
        this.fansCount = fansCount
    }

    static find(limit = 0) {
        if (limit > 0) {
            const listTemp = storage.sort(function(a, b) {
                return b.id - a.id
            })
            const list = listTemp.slice(0, limit)
        } else {
            const list = storage
        }

        const new_list = list.map(item => {
            const temp = {}
            Object.assign(temp, item)
                // lodash
            temp.avatar = Upload.getUrl(temp.avatar)
            return temp
        })

        return new_list
    }

    static findOne(id) {
        const userTemp = storage.find(s => s.id === id)
        if (userTemp) {
            const User = {}
            Object.assign(User, userTemp)
            User.avatar = Upload.getUrl(User.avatar)
            return User
        }
        return userTemp
    }

    delete() {
        return storage.splice(storage.indexOf(this), 1)
    }

    save() {
        storage.indexOf(this) === -1 && storage.push(this)
        return true
    }

    update() {
        return true
    }
}

const storage = [
    new UserDemo(1, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(2, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(3, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(4, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(5, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(6, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(7, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(8, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(9, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(10, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(11, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(12, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(13, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(14, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(15, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(16, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(17, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(18, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(19, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
    new UserDemo(20, '周杰伦', 'jyqj.png', '橘子角逐', 23222, 232323),
]

module.exports = UserDemo